﻿using NexC_Sharp.Services;
using System.Windows.Controls;


namespace RobotPerCeption.ViewModel
{

    public enum RoboteModelEnum
    {
        //模式 0：示教 1：远程 2：运行
        TELNET = 1, SIJIAO = 0, RUNING = 2
    }



    /// <summary>
    /// 用于子线程监听 示教模式状态
    /// </summary>
    public class RoboteModelStatusLinsten
    {
        public static CancellationTokenSource cts = null;
        public static CancellationToken token { set; get; }

        public static bool tag { set; get; } //停止进程

        private Thread thread { get; set; }


        public Thread GetThread()
        {
            return this.thread;
        }

        public RoboteModelStatusLinsten(Label label, RadioButton runing, RadioButton sijiao, RadioButton telnet)
        {
            cts = new CancellationTokenSource();
            token = cts.Token;
            Console.WriteLine("struct function token hash" + token.GetHashCode());

            thread = new Thread(() => modelLinsten(cts, label, runing, sijiao, telnet));

        }


        public void linstenRun()
        {

            if (this.thread != null)
            {

                this.thread.Start();
            }
        }


        protected void modelLinsten(CancellationTokenSource cts, Label labStatus, RadioButton runing, RadioButton sijiao, RadioButton telnet)
        {

            while (!token.IsCancellationRequested)
            {

                int response = Nex.get_current_mode(MainWindow.roboteName);
                Thread.Sleep(3000);
                Console.WriteLine("modelLinsten" + this.GetHashCode());
                //表示未连接
                if (response != -2)
                {

                    RoboteModelEnum t = (RoboteModelEnum)response;
                    //匹配状态   lable赋值
                    if (t == RoboteModelEnum.SIJIAO)
                    {
                        labStatus.Dispatcher.BeginInvoke(new Action(() => { labStatus.Content = "示教模式"; }));
                        sijiao.Dispatcher.BeginInvoke(new Action(() => { sijiao.IsChecked = true; }));

                    }
                    else if (t == RoboteModelEnum.TELNET)
                    {
                        labStatus.Dispatcher.BeginInvoke(new Action(() => { labStatus.Content = "远程模式"; }));
                        telnet.Dispatcher.BeginInvoke(new Action(() => { telnet.IsChecked = true; }));
                    }
                    else if (t == RoboteModelEnum.RUNING)
                    {
                        labStatus.Dispatcher.BeginInvoke(new Action(() => { labStatus.Content = "运行模式"; }));
                        runing.Dispatcher.BeginInvoke(new Action(() => { runing.IsChecked = true; }));

                    }


                }
            }

        }

        /// <summary>
        /// 线程停止
        /// </summary>
        /// <param name="cur_cts">token 源</param>
        /// <param name="source">source 源</param>
        /// <returns></returns>
        public static bool stopThread(CancellationToken cur_cts, CancellationTokenSource source)
        {
            if (cur_cts == null || source == null)
            {
                //拒绝停止 
                return false;
            }
            if (!cur_cts.IsCancellationRequested)
            {
                source.Cancel();
            }
            Thread.Sleep(1000);
            return true;
        }
    }
}
